﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Telerik.Windows.Controls;
using System.Windows.Threading;
using Zeus.Classes;
using Zeus.Classes.Documents;
using System.ComponentModel;
using Zeus.Classes.Events;
using Zeus.Events;
using Zeus.UserControls;
using Zeus.Classes.Views;

namespace Zeus.UserControls
{
    public partial class SolutionExplorerUserControl : UserControl
    {
        private readonly DispatcherTimer _doubleClickTimer = new DispatcherTimer();
        private bool _doubleClickFlag;
       
        public SolutionExplorerUserControl()
        {
            InitializeComponent();
            _doubleClickTimer.Interval = TimeSpan.FromMilliseconds(500);
            _doubleClickTimer.Tick += DoubleClickTimerTick;
            treeView1.AddHandler(MouseLeftButtonDownEvent, new MouseButtonEventHandler(TreeView1MouseLeftButtonDown), true);
            App.EventAggegator.GetEvent<UpDateSolution>().Subscribe(OnProjectCreated);
            App.EventAggegator.GetEvent<ViewSolutionExplorer>().Subscribe(ChangeState);
            App.EventAggegator.GetEvent<CloseSolution>().Subscribe(CloseAll);
            
        }

        private void CloseAll(object obj)
        {
            ProjectItems.Header = "Project Item";
            App_Code.Items.Clear();
            Meshes.Items.Clear();
            Sounds.Items.Clear();
            Videos.Items.Clear();
            Images.Items.Clear();
            Others.Items.Clear();
            Scenes.Items.Clear();
        }

        public void ChangeState(object o)
        {
            SolutionExplorerAccordion.Visibility = Visibility.Visible;
        }

        private void OnProjectCreated(object obj)
        {
            FillSolution();
        }

        void TreeView1MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
                RadTreeViewItem selectedItem = treeView1.SelectedItem as RadTreeViewItem;
                if (selectedItem != null && selectedItem.Items.Count == 0)
                {
                    if (this._doubleClickFlag)
                    {
                        int i = int.Parse(selectedItem.DataContext.ToString());
                        switch (Project.ProjectDocuments[i].DocType )
                        {
                            case ProjectItem_Types.Image:
                                {
                                    EditImageView view = new EditImageView((ImageDocument)Project.ProjectDocuments[i]);
                                    Project.ProjectDocuments[i].Lview.Add(view);
                                    view.Render();
                                    break;
                                }
                            case ProjectItem_Types.Sound:
                                {
                                    EditSoundView view = new EditSoundView((SoundDocument) Project.ProjectDocuments[i]);
                                    Project.ProjectDocuments[i].Lview.Add(view);
                                    view.Render();
                                    break;
                                }
                            case ProjectItem_Types.Video:
                                {
                                    EditVideoView view = new EditVideoView((videoDocument)Project.ProjectDocuments[i]);
                                    Project.ProjectDocuments[i].Lview.Add(view);
                                    view.Render();
                                    break;
                                }
                            case ProjectItem_Types.Text:
                                {
                                    TextEditorView view = new TextEditorView((TextDocument)Project.ProjectDocuments[i]);
                                    Project.ProjectDocuments[i].Lview.Add(view);
                                    view.Render();
                                    break;
                                }
                            case ProjectItem_Types.Class:
                                {
                                    TextEditorView view = new TextEditorView((TextDocument)Project.ProjectDocuments[i]);
                                    Project.ProjectDocuments[i].Lview.Add(view);
                                    view.Render();
                                    break;
                                }
                            case ProjectItem_Types.Scene:
                                {
                                    SceneView view = new SceneView((SceneDocument)Project.ProjectDocuments[i]);
                                    Project.ProjectDocuments[i].Lview.Add(view);
                                    view.Render();
                                    break;
                                }
                        }
                        //App.EventAggegator.GetEvent<AddNewTab>().Publish(Project.ProjectDocuments[i]);
                    }
                    else
                    {
                        _doubleClickTimer.Start();
                        _doubleClickFlag = true;
                    }
                }
            }
            catch (Exception)
            {}
        }
        // The double-click interval has expired, therefore stop the timer and lower the flag
        void DoubleClickTimerTick(object sender, EventArgs e)
        {
            this._doubleClickTimer.Stop();
            this._doubleClickFlag = false;
        }

        private void FillSolution()
        {
            SolutionExplorerAccordion.Visibility = Visibility.Visible;
            App_Code.Items.Clear();
            Meshes.Items.Clear();
            Sounds.Items.Clear();
            Videos.Items.Clear();
            Images.Items.Clear();
            Others.Items.Clear();
            Scenes.Items.Clear();
            References.Items.Clear();
            try
            {
                ProjectItems.Header = Project.ProjectName;
                for (int i = 0; i < Project.ProjectDocuments.Count; i++)
                {
                    RadTreeViewItem item = new RadTreeViewItem();
                    item.Header = Project.ProjectDocuments[i].FileName;
                    item.DataContext = Project.ProjectDocuments[i].Doc_ID;
                    switch (Project.ProjectDocuments[i].DocType)
                    {
                        case ProjectItem_Types.Class:
                            {
                                App_Code.Items.Add(item);
                                break;
                            }
                        case ProjectItem_Types.Image:
                            {
                                Images.Items.Add(item);
                                break;
                            }
                        case ProjectItem_Types.Mesh:
                            {
                                Meshes.Items.Add(item);
                                break;
                            }
                        case ProjectItem_Types.Sound:
                            {
                                Sounds.Items.Add(item);
                                break;
                            }
                        case ProjectItem_Types.Scene:
                            {
                                Scenes.Items.Add(item);
                                break;
                            }
                        case ProjectItem_Types.Video:
                            {
                                Videos.Items.Add(item);
                                break;
                            }
                        case ProjectItem_Types.Text:
                            {
                                Others.Items.Add(item);
                                break;
                            }
                        case ProjectItem_Types.Reference:
                            {
                                References.Items.Add(item);
                                break;
                            }
                    }
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }
    }
}
